package edu.uci.hm.util;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.StringTokenizer;

public class Util {
	public final static long anonymousBase = 1L << 32;

	// DON'T USE THIS ONE TO CHANGE IP TO ID
	// USE getUID
	public static long ip2Long(String ip) {
		try {
			StringTokenizer st = new StringTokenizer(ip, ".");
			long ip1 = Integer.valueOf(st.nextToken());
			long ip2 = Integer.valueOf(st.nextToken());
			long ip3 = Integer.valueOf(st.nextToken());
			long ip4 = Integer.valueOf(st.nextToken());

			long totalIP = (long) (ip1 * (Math.pow(256, 3)) + ip2
					* (Math.pow(256, 2)) + ip3 * (Math.pow(256, 1)) + ip4);

			return (totalIP);
		} catch (Exception e) {
			return 0;
		}
	}

	public static void main(String[] args) {
		long ip = ip2Long("213.253.39.175");
		long base = 1L << 32;
		ip += base;
		System.out.println(ip);
	}

	public static void putLongInByteArray(long l, byte[] array, int offset) {
		int i, shift;
		for (i = 0, shift = 56; i < 8; i++, shift -= 8)
			array[offset + i] = (byte) (0xFF & (l >> shift));
	}

	public static void putIntInByteArray(int value, byte[] array, int offset) {
		for (int i = 0; i < 4; i++) {
			int off = (3 - i) * 8;
			array[offset + i] = (byte) ((value >>> off) & 0xFF);
		}
	}

	public static void putFloatInByteArray(float f, byte[] array, int offset) {
		int i = Float.floatToIntBits(f);
		putIntInByteArray(i, array, offset);
	}

	public static void putDoubleInByteArray(double d, byte[] array, int offset) {
		long l = Double.doubleToRawLongBits(d);
		putLongInByteArray(l, array, offset);
	}

	public static long toLong(byte[] bytearray, int offset) {
		long result = 0;
		for (int i = offset; i < offset + 8 /* Bytes in long */; i++) {
			result = (result << 8 /* Bits in byte */)
					| (0xff & (byte) (bytearray[i] & 0xff));
		}
		return result;
	}

	public static int toInt(byte[] b, int offset) {
		int value = 0;
		for (int i = 0; i < 4; i++) {
			int shift = (4 - 1 - i) * 8;
			value += (b[i + offset] & 0x000000FF) << shift;
		}
		return value;
	}

	public static float toFloat(byte[] data, int offset) {
		int i = toInt(data, offset);
		return Float.intBitsToFloat(i);
	}

	public static double toDouble(byte[] data, int offset) {
		long l = toLong(data, offset);
		return Double.longBitsToDouble(l);
	}

	public static int getMinutes(String timestamp) {
		int year = Integer.parseInt(timestamp.substring(0, 4));
		int month = Integer.parseInt(timestamp.substring(5, 7));
		int day = Integer.parseInt(timestamp.substring(8, 10));
		int hour = Integer.parseInt(timestamp.substring(11, 13));
		int min = Integer.parseInt(timestamp.substring(14, 16));
		int seconds = Integer.parseInt(timestamp.substring(17, 19));
		Calendar cal = new GregorianCalendar(year, month - 1, day, hour, min,
				seconds);
		return (int) (cal.getTime().getTime() / 60000L);
	}

	// use this one to change ip to id
	public static long getUid(String ip) {
		try {
			StringTokenizer st = new StringTokenizer(ip, ".");
			long ip1 = Integer.valueOf(st.nextToken());
			long ip2 = Integer.valueOf(st.nextToken());
			long ip3 = Integer.valueOf(st.nextToken());
			long ip4 = Integer.valueOf(st.nextToken());
			long totalIP = (long) (ip1 * 256 * 256 * 256) + ip2 * 256 * 256
					+ ip3 * 256 + ip4;
			return totalIP + anonymousBase;
		} catch (Exception e) {
			return anonymousBase;
		}
	}

	public static boolean isAnononymousUid(long uid) {
		return (uid >> 32) == 1;
	}

	public static String[] toArray(List<String> input) {
		String[] output = new String[input.size()];
		for (int index = 0; index < input.size(); index++) {
			output[index] = input.get(index);
		}
		return output;
	}

}
